<template>
  <div id="home">
    <nav-bar class="home-nav"><div slot="mid">购物街</div></nav-bar>
    <scroll
      class="content"
      ref="scroll"
      :probe-type="3"
      :pull-up-load="true"
      @scroll="contentScroll"
      @upLoad="loadMore"
    >
      <home-swiper :banners="banners" @swiperImageLoad="swiperImageLoad" />
      <home-recommend :recommend="recommend" />
      <feature></feature>
      <tab-control
        :titles="['流行', '新款', '精选']"
        @itemClick="itemClick"
        ref="tabControl"
      ></tab-control>
      <goods-list :goods="goodsType"></goods-list>
    </scroll>
    <tab-control
      :titles="['流行', '新款', '精选']"
      class="tab-control"
      @itemClick="itemClick"
      ref="cloneTabControl"
      v-show="isCloneTabControlShow"
    ></tab-control>
    <back-top @click.native="backtop" v-show="isShowBackTop"></back-top>
  </div>
</template>

<script>
import HomeSwiper from "./childComponents/HomeSwiper.vue";
import HomeRecommend from "./childComponents/HomeRecommend.vue";
import Feature from "./childComponents/Feature.vue";

import NavBar from "../../components/common/navbar/NavBar.vue";
import TabControl from "../../components/content/tabControl/TabControl.vue";
import GoodsList from "../../components/content/goods/GoodsList.vue";

import { getHomeMultidata, getHomeGoods } from "../../network/home";

import Scroll from "../../components/common/scroll/Scroll.vue";
import BackTop from "../../components/content/backTop/BackTop.vue";

export default {
  name: "Home",
  components: {
    NavBar,
    HomeSwiper,
    HomeRecommend,
    Feature,
    TabControl,
    GoodsList,
    Scroll,
    BackTop,
  },
  data() {
    return {
      banners: [],
      dKeyword: [],
      keywords: [],
      recommend: [],
      goods: {
        pop: { page: 0, list: [] },
        new: { page: 0, list: [] },
        sell: { page: 0, list: [] },
      },
      currentType: "pop",
      isShowBackTop: false,
      tabOffstTop: 0,
      isCloneTabControlShow: false,
      // saveY: 0,
    };
  },
  created() {
    this.getHomeMultidata();

    // 流行、新款、精选请求数据
    this.getHomeGoods("pop");
    this.getHomeGoods("new");
    this.getHomeGoods("sell");
  },
  computed: {
    goodsType() {
      return this.goods[this.currentType].list;
    },
    destroyed() {
      console.log("?");
    },
    activate() {
      // this.$refs.scroll.scrollTo(0, this.saveY, 0);
      console.log("?");
    },
    deactivated() {
      // this.saveY = this.$refs.scroll.getSaveY();
      console.log("?");
    },
  },

  methods: {
    // 分类显示点击
    itemClick(index) {
      console.log(index);
      switch (index) {
        case 0:
          this.currentType = "pop";
          break;
        case 1:
          this.currentType = "new";
          break;
        case 2:
          this.currentType = "sell";
          break;
      }
      this.$refs.tabControl.currentIndex = index;
      this.$refs.cloneTabControl.currentIndex = index;
    },

    // 返回顶部按钮
    backtop() {
      this.$refs.scroll.scrollTo(0, 0, 500);
    },

    // 上拉加载更多
    loadMore() {
      this.getHomeGoods(this.currentType);
    },

    // 距离显示顶部按钮
    contentScroll(position) {
      this.isShowBackTop = -position.y > 1000;
      this.isCloneTabControlShow = position.y <= -this.tabOffstTop;
    },

    // 监听轮播图加载
    swiperImageLoad() {
      console.log(this.$refs.tabControl.$el.offsetTop);
      this.tabOffstTop = this.$refs.tabControl.$el.offsetTop;
    },

    getHomeMultidata() {
      getHomeMultidata().then((res) => {
        // console.log(res);
        this.banners = res.data.data.banner.list;
        this.recommend = res.data.data.recommend.list;
      });
    },
    getHomeGoods(type) {
      const page = this.goods[type].page + 1;
      getHomeGoods(type, page).then((res) => {
        // console.log(res);
        for (let n of res.data.data.list) {
          this.goods[type].list.push(n);
        }
        // this.goods[type].list.push(...res.data.data.list);
        this.goods[type].page += 1;

        this.$refs.scroll.finishedPullUp();
      });
    },
  },
  mounted() {},
};
</script>

<style scoped>
.home-nav {
  /* position: fixed; */
  /* position: sticky;
  top: 0; */
  /* left: 0;
  right: 0; */
  background-color: var(--color-tint);
  color: white;
  z-index: 9;
}
.tab-control {
  /* position: sticky;
  top: 44px; */
  position: relative;
  z-index: 9;
}
.content {
  overflow: hidden;

  position: absolute;
  top: 44px;
  bottom: 49px;
  left: 0;
  right: 0;
  /* height: calc(100% - 93px);
  overflow: hidden; */
}
#home {
  height: 100vh;
}
</style>
